package com.leon.android.arch.mvvv.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.apkfuns.logutils.LogUtils
import com.leon.android.common.utils.FragmentUserVisibleController

abstract class BaseFragment : Fragment() {

    protected var TAG = this.javaClass.simpleName

//    /**
//     * 此类用来解决fragment在viewpager中预加载导致的显示隐藏状态混乱的问题
//     * */
//    private val userVisibleController by lazy {
//        FragmentUserVisibleController(this, this)
//    }

    abstract fun getLayoutID():Int
    abstract fun initView()
    abstract fun initData()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        LogUtils.d("$TAG : onCreateView")
        return inflater.inflate(getLayoutID(), container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initData()
        bindModel()
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
//        userVisibleController.activityCreated()
        LogUtils.d("$TAG : onActivityCreated")
    }

    override fun onResume() {
        super.onResume()
//        userVisibleController.resume()
        LogUtils.d("$TAG : onResume")

    }

    override fun onPause() {
        super.onPause()
//        userVisibleController.pause()
        LogUtils.d("$TAG : onPause")
    }


    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
//        userVisibleController.setUserVisibleHint(isVisibleToUser)
        LogUtils.d("$TAG : setUserVisibleHint $isVisibleToUser")

    }

//    override fun setWaitingShowToUser(waitingShowToUser: Boolean) {
//        userVisibleController.isWaitingShowToUser = waitingShowToUser
//    }
//
//    override fun isWaitingShowToUser(): Boolean {
//        return userVisibleController.isWaitingShowToUser
//    }
//
//    override fun isVisibleToUser(): Boolean {
//        return userVisibleController.isVisibleToUser
//    }
//
//    override fun callSuperSetUserVisibleHint(isVisibleToUser: Boolean) {
//        super.setUserVisibleHint(isVisibleToUser)
//    }

    protected open fun getViewModelFactory(): ViewModelProvider.Factory? {
        return null
    }
//
//    override fun onVisibleToUserChanged(isVisibleToUser: Boolean, invokeInResumeOrPause: Boolean) {
//
//    }

    protected abstract fun bindModel()

}